﻿<?php
/*
 * @Description 易宝支付非银行卡支付专业版接口范例 
 * @V3.0
 * @Author rui.xin
 */			
function getReqHmacString($p0_Cmd,$p2_Order,$p3_Amt,$p8_Url,$pa_MP,$pa7_cardNo,$pa8_cardPwd,$pd_FrpId,$pa0_Mode,$pr_NeedResponse)
{
	#进行加密串处理，一定按照下列顺序进行
	$sbOld		=	"";
	#加入业务类型
	$sbOld		=	$sbOld.$p0_Cmd;
	#加入商户代码
	$sbOld		=	$sbOld.C('P1_MERID');
	#加入商户订单号
	$sbOld		=	$sbOld.$p2_Order;
	#加入支付卡面额
	$sbOld		=	$sbOld.$p3_Amt;
	#加入商户接收交易结果通知的地址
	$sbOld		=	$sbOld.$p8_Url;
	#加入商户扩展信息
	$sbOld 		= $sbOld.$pa_MP;
	#加入支付卡序列号
	$sbOld		=	$sbOld.$pa7_cardNo;
	#加入支付卡密码
	$sbOld		=	$sbOld.$pa8_cardPwd;
	#加入银行编码
	$sbOld		=	$sbOld.$pd_FrpId;
	#加入支付卡支付传递模式
	$sbOld		=	$sbOld.$pa0_Mode;
	#加入应答机制
	$sbOld		=	$sbOld.$pr_NeedResponse;
	#echo "localhost:".$sbOld;
	return HmacMd5($sbOld,C('MERCHANTKEY'));
    
} 
function annulCard($p2_Order,$p3_Amt,$p8_Url,$pa_MP,$pa7_cardNo,$pa8_cardPwd,$pd_FrpId)
{ 	
	# 非银行卡支付专业版支付请求，固定值 "AnnulCard".		
	$p0_Cmd					= "AnnulCard";

	#固定值 "1"
	$pa0_Mode				=	"1";

	#应答机制.为"1": 需要应答机制;为"0": 不需要应答机制.			
	$pr_NeedResponse	= "1";
	
	# 非银行卡支付专业版请求地址,无需更改.
	$reqURL_SNDApro		= "https://www.yeepay.com/app-merchant-proxy/command.action";
	
	#调用签名函数生成签名串
	$hmac	= getReqHmacString($p0_Cmd,$p2_Order,$p3_Amt,$p8_Url,$pa_MP,$pa7_cardNo,$pa8_cardPwd,$pd_FrpId,$pa0_Mode,$pr_NeedResponse);
	
	#进行加密串处理，一定按照下列顺序进行
	$params = array(
		#加入业务类型
		'p0_Cmd'						=>	$p0_Cmd,
		#加入商家ID
		'p1_MerId'					=>	C('P1_MERID'),
		#加入商户订单号
		'p2_Order' 					=>	$p2_Order,
		#加入支付卡面额
		'p3_Amt'						=>	$p3_Amt,
		#加入商户接收交易结果通知的地址
		'p8_Url'						=>	$p8_Url,
		#加入商户扩展信息
		'pa_MP'					  	=> 	$pa_MP,
		#加入支付卡序列号
		'pa7_cardNo'				=>	$pa7_cardNo,
		#加入支付卡密码
		'pa8_cardPwd'				=>	$pa8_cardPwd,
		#加入银行编码
		'pd_FrpId'					=>	$pd_FrpId,
		#加入支付卡支付传递模式
		'pa0_Mode'					=>	$pa0_Mode,
		#加入应答机制
		'pr_NeedResponse'		=>	$pr_NeedResponse,
		#加入校验码
		'hmac' 							=>	$hmac);
	
	$pageContents	= HttpClient::quickPost($reqURL_SNDApro, $params);  
	//echo "pageContents:".$pageContents;	
}

function getCallbackHmacString($r0_Cmd,$r1_Code,$rb_Order,$r2_TrxId,$rq_CardNo,$rq_ReturnMsg,$pa_MP,$rc_Amt)
{
	#进行校验码检查 取得加密前的字符串
	$sbOld="";
	#加入业务类型
	$sbOld = $sbOld.$r0_Cmd;
	#加入支付结果
	$sbOld = $sbOld.$r1_Code;
	#加入商户编号
	$sbOld = $sbOld.C('P1_MERID');
	#加入商户订单号
	$sbOld = $sbOld.$rb_Order;
	#加入易宝支付交易流水号
	$sbOld = $sbOld.$r2_TrxId;
	#加入商户扩展信息
	$sbOld = $sbOld.$pa_MP;
	#加入支付金额
	$sbOld = $sbOld.$rc_Amt;                
            	
	echo "[".$sbOld."]";
  
	return HmacMd5($sbOld,C('MERCHANTKEY'));

}
#取得返回串中的所有参数.
function getCallBackValue(&$r0_Cmd,&$r1_Code,&$rb_Order,&$r2_TrxId,&$rq_CardNo,&$rq_ReturnMsg,&$pa_MP,&$rc_Amt,&$hmac)
{  
	$r0_Cmd			= $_REQUEST['r0_Cmd'];
	$r1_Code		= $_REQUEST['r1_Code'];
	$rb_Order		= $_REQUEST['rb_Order'];
	$r2_TrxId		= $_REQUEST['r2_TrxId'];
	$pa_MP			= $_REQUEST['pa_MP'];
	$rq_CardNo  = $_REQUEST['rq_CardNo'];
	$rq_ReturnMsg= $_REQUEST['rq_ReturnMsg'];
	$rc_Amt			= $_REQUEST['rc_Amt'];
	$hmac			= $_REQUEST['hmac'];
	
	return null;
	
}
#验证返回参数中的hmac与商户端生成的hmac是否一致.
function CheckHmac($r0_Cmd,$r1_Code,$rb_Order,$r2_TrxId,$rq_CardNo,$rq_ReturnMsg,$pa_MP,$rc_Amt,$hmac)
{

	if($hmac==getCallbackHmacString($r0_Cmd,$r1_Code,$rb_Order,$r2_TrxId,$rq_CardNo,$rq_ReturnMsg,$pa_MP,$rc_Amt))
		return true;
	else
		return false;
		
}  
function HmacMd5($data,$key)           
{                                      
	# RFC 2104 HMAC implementation for php.
	# Creates an md5 HMAC.                 
	# Eliminates the need to install mhash to compute a HMAC
	# Hacked by Lance Rushing(NOTE: Hacked means written)
	                                       
	#需要配置环境支持iconv，否则中文参数不能正常处理
	#$key = iconv("GB2312","UTF-8",$key);  
	#$data = iconv("GB2312","UTF-8",$data);
	                                       
	$b = 64; # byte length for md5         
	if (strlen($key) > $b) {               
	$key = pack("H*",md5($key));           
	}                                      
	$key = str_pad($key, $b, chr(0x00));   
	$ipad = str_pad('', $b, chr(0x36));    
	$opad = str_pad('', $b, chr(0x5c));    
	$k_ipad = $key ^ $ipad ;               
	$k_opad = $key ^ $opad;                
	                                       
	return md5($k_opad . pack("H*",md5($k_ipad . $data)));	
}



?> 